﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using TechEd.Data.Core.Entity;
using TechEd.Rest.Core.Interfaces;
using TechEd.RestService.DataLayer.Communication;

namespace TechEd.RestService
{
	// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
	public class Service1 : IService1
	{
		public string GetData(int value)
		{
			return string.Format("You entered: {0}", value);
		}

		public VideoInfo UploadVideo(string prefix, string fileName, byte[] bytes, string contentType)
		{
			VideoInfoContext context = new VideoInfoContext(prefix);
			var blobAddress = context.UploadVideo(fileName, bytes, contentType);
			VideoInfo vi = new VideoInfo()
			{
				Name = "TestVideo",
				ShotDescription = "Length: 5:49",
				Title = "Offspring",
				Params = new[]
													         	{
													         		new VideoParams()
													         			{
													         				BlobAddressUri = blobAddress
													         			}
													         	}
			};
			context.Insert(vi);

			return vi;
		}

		public void Test()
		{
			string prefix = "TechEdTest";
			using (FileStream fs = new FileStream(@"D:\Video\Music\Offspring\Offspring_-_Can't_Repeat.avi", FileMode.Open))
			{
				var length = fs.Length;
				if (length > int.MaxValue)
					throw new ApplicationException("File length more than int.MaxValue");

				var bytes = new byte[length];

				fs.Read(bytes, 0, (int) length);

				VideoInfoContext context = new VideoInfoContext(prefix);
				var blobAddress = context.UploadVideo("Offspring_-_Can't_Repeat.avi", bytes, "video/x-msvideo");
				VideoInfo vi = new VideoInfo()
				               	{
				               		Name = "TestVideo", 
													ShotDescription = "Length: 5:49", 
													Title = "Offspring", 
													Params = new []
													         	{
													         		new VideoParams()
													         			{
													         				BlobAddressUri = blobAddress
													         			}
													         	}
				               	};
				context.Insert(vi);

				var videos = context.GetAll();
			}

/*			
			try
			{
				ProductKindContext context = new ProductKindContext(prefix);
				var p1 = context.GetAll();
				ProductKind pr = new ProductKind() { Name = "Общий" };
				context.Insert(pr);
				var p2 = context.GetAll();
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}*/
		}


		public CompositeType GetDataUsingDataContract(CompositeType composite)
		{
			if (composite == null)
			{
				throw new ArgumentNullException("composite");
			}
			if (composite.BoolValue)
			{
				composite.StringValue += "Suffix";
			}
			return composite;
		}
	}
}
